package com.dianrui.fmi.activity;

import android.annotation.SuppressLint;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

import com.dianrui.fmi.Constant;
import com.dianrui.fmi.R;

import butterknife.BindView;
import butterknife.OnClick;

public class WebviewActivity extends BaseActivity {
    private String explainId;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.webview)
    WebView webview;

    @Override
    public int getContentViewId() {
        return R.layout.activity_webview;
    }

    @SuppressLint({"SetJavaScriptEnabled", "SetTextI18n", "ClickableViewAccessibility"})
    @Override
    public void initView() {
        if (getIntent() != null && getIntent().getExtras() != null) {
            explainId=getIntent().getStringExtra("explainId");
        }
        webview.loadUrl(Constant.EXPLAIN+explainId);
        WebSettings webSetting = webview.getSettings();
        webSetting.setJavaScriptEnabled(true);
        webSetting.setUseWideViewPort(true);
        webSetting.setLoadWithOverviewMode(true);
        webSetting.setDomStorageEnabled(true);
        webview.setWebChromeClient(wcc);

    }

    WebChromeClient wcc = new WebChromeClient() {
        @Override
        public void onReceivedTitle(WebView view, String titles) {
            super.onReceivedTitle(view, titles);
            title.setText(titles);
        }
    };

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }

    @Override
    protected void onDestroy() {
        if (webview != null) {
            webview.clearCache(true);
            webview.stopLoading();
            //防止系统在3.x的手机，在缩放按钮未完全关闭时候退出的闪退
            webview.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (webview != null) {
                        webview.destroy();
                        webview = null;
                    }
                }
            }, 3000);
        }
        super.onDestroy();
    }
}
